package cn.edu.xmu.oomall.liquidation.model.bo.liquid.revenue;

import cn.edu.xmu.oomall.liquidation.dao.LiquidationDao;
import cn.edu.xmu.oomall.liquidation.microservice.AfterSaleService;
import cn.edu.xmu.oomall.liquidation.microservice.OrderService;
import cn.edu.xmu.oomall.liquidation.microservice.vo.AfterSaleRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.OrderItemRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.PaymentRetVo;
import cn.edu.xmu.oomall.liquidation.model.bo.Liquidation;
import cn.edu.xmu.oomall.liquidation.model.bo.RevenueItem;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.CalcTotal;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class AfterSalePaymentLiquid extends BasePaymentLiquid {
    @Autowired
    private OrderService orderService;

    @Autowired
    private AfterSaleService afterSaleService;

    @Override
    protected CalcTotal liquidateSinglePayment(PaymentRetVo payment, LocalDateTime liquidDateTime) {
        CalcTotal platformCalcTotal = new CalcTotal(liquidUtil.getPlatformCalcFieldList());
        String afterSaleSn = payment.getDocumentId();
        InternalReturnObject<AfterSaleRetVo> afterSaleObj = afterSaleService.getAfterSaleBySn(afterSaleSn);
        AfterSaleRetVo afterSale = afterSaleObj.getData();
        Liquidation liquid = liquidUtil.getLiquid(afterSale.getShopId(), liquidDateTime);
        Long liquidId = liquid.getId();

        CalcTotal calcTotal = liquidateAfterSale(afterSale, liquidId, payment);

        //更新清算表
        calcTotal.changeLiquid(liquid);
        liquidUtil.updateLiquid(liquid);

        //更新平台统计
        platformCalcTotal.add(calcTotal);

        return platformCalcTotal;
    }


    private CalcTotal liquidateAfterSale(AfterSaleRetVo afterSale, Long liquidId, PaymentRetVo payment) {
        CalcTotal calcTotal = new CalcTotal();

        Long orderItemId = afterSale.getOrderItemId();
        InternalReturnObject<OrderItemRetVo> orderItemObj = orderService.getOrderItemById(orderItemId);
        OrderItemRetVo orderItem = orderItemObj.getData();

        Long shopRevenue = payment.getAmount();

        RevenueItem revenueItem = new RevenueItem();
        revenueItem.setLiquidId(liquidId);
        revenueItem.setPaymentId(payment.getId());
        revenueItem.setShopId(afterSale.getShopId());
        revenueItem.setOrderId(afterSale.getOrderId());
        revenueItem.setOrderitemId(orderItemId);
        revenueItem.setProductId(orderItem.getProductId());
        revenueItem.setProductName(liquidUtil.getProductNameById(orderItem.getProductId()));
        revenueItem.setAmount(payment.getAmount());
        revenueItem.setQuantity(afterSale.getQuantity());

        //将全部金额清算给商家
        revenueItem.setShopRevenue(shopRevenue);

        liquidUtil.insertRevenue(revenueItem);

        calcTotal.add("shopRevenue", shopRevenue);
        return calcTotal;
    }
}
